using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sueetie.Wiki;

public partial class Register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetActiveButtonAttributes(CreateUserButton);
            if (Request.IsAuthenticated)
                Response.Redirect("../default.aspx");
        }
    }

    private void SetActiveButtonAttributes(Button button)
    {
        button.Attributes.CssStyle.Add("background-color", "#eee");
        button.Attributes.Add("OnMouseOut", "this.style.backgroundColor ='#eee';");
        button.Attributes.CssStyle.Add("text-decoration", "none");
        button.Attributes.CssStyle.Add("margin", "3px");
        button.Attributes.CssStyle.Add("padding", "7px 6px 7px 6px");
        button.Attributes.Add("OnMouseOver", "this.style.backgroundColor ='#ddd';");
    }

    protected void Validate_Username(object sender, ServerValidateEventArgs args)
    {
        args.IsValid = (!txtUsername.Text.Trim().Contains(" "));
    }

    protected void CreateUser_Click(object sender, System.EventArgs e)
    {
        if (Page.IsValid && IsPostBack)
        {
            if (Membership.GetUser(txtUsername.Text) == null && Membership.GetUserNameByEmail(txtEmailAddress.Text) == null)
            {
                FormsAuthentication.SetAuthCookie(txtUsername.Text, true);

                HttpCookie cookie = Request.Cookies["SueetieUserProfile"];
                if (cookie == null)
                    cookie = new HttpCookie("SueetieUserProfile");

                cookie["Email"] = txtEmailAddress.Text;
                cookie["Name"] = txtDisplayName.Text;
                cookie["EnableDisplayName"] = "true";

                Response.Cookies.Add(cookie);


                MembershipUser user = Membership.CreateUser(txtUsername.Text, txtPassword1.Text, txtEmailAddress.Text);

                ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(txtUsername.Text, true);

                profile.Personal.DisplayName = txtDisplayName.Text;
                profile.Save();

                // SUEETIE NOTE: [BLOG]  BlogEngine.NET will throw an error if authorizing a user and they do not appear in be_User table.
                // When Blog Application added, uncomment these lines.  

                beDataContext dataContext = new beDataContext();

                be_User _be_User = new be_User();
                _be_User.UserName = user.UserName;
                _be_User.Password = string.Empty;
                _be_User.LastLoginTime = DateTime.Now;
                _be_User.EmailAddress = user.Email;
                dataContext.be_Users.InsertOnSubmit(_be_User);
                dataContext.SubmitChanges();

         
                Roles.AddUserToRole(user.UserName, "Registered");
                string returnUrl = Request.QueryString["ReturnUrl"];

                // SUEETIE NOTE: [WIKI]  Adding user to ScrewTurn Wiki /public/users.cs. 

                WikiUsers.AddUser(txtUsername.Text, "sueetie12345", txtEmailAddress.Text, null);

                // SUEETIE NOTE: [WIKI]  Adding user to ScrewTurn Wiki Public Groups /groups/[groupName]/wiki/public/users.cs. 
                //  All public groups share the same global password with site wiki.  groupName must match group directory name.

                WikiUsers.AddUser(txtUsername.Text, "sueetie12345", txtEmailAddress.Text, "demo");
          
                if (returnUrl != null)
                    Response.Redirect(returnUrl);
                else
                    Response.Redirect("welcome.aspx", true);
            }
            else
            {
                string loginUrl = "login.aspx";
                if (Request.QueryString["ReturnUrl"] != null)
                    loginUrl += "?ReturnUrl=" + Request.QueryString["ReturnUrl"];

                if (Membership.GetUser(txtUsername.Text) != null)
                    labelUserMessage.Text = "User already exists.  Please enter another username.  If you already have a Sueetie account, please <a href=\"" + loginUrl + "\">login here.</a>";

                else if (Membership.GetUserNameByEmail(txtEmailAddress.Text) != null)
                    labelUserMessage.Text = "The email address already exists.  Do you already have a Sueetie account?  If so, please <a href=\"" + loginUrl + "\">login here.</a>";

            }
        }
    }
}
